import model.User;
import org.junit.Test;

import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;

public class CRUD {

    @Test
    public void Statement_executeQuery() {
        Connection connection = null;
        try {
            connection = 连接.getConnection();
            Statement statement = connection.createStatement();

            /**
             * Statement 中包含的方法：
             *     int excuteUpdate(String sql)：执行更新操作INSERT、UPDATE、DELETE
             *     ResultSet executeQuery(String sql)：执行查询操作SELECT
             */

            String sql = "SELECT * FROM user;";

            ResultSet resultSet = statement.executeQuery(sql);

            ArrayList<User> users = new ArrayList<>();
            while (resultSet.next()) {
                User user = new User(resultSet.getInt(1), resultSet.getString(2), resultSet.getString(3));
                users.add(user);
            }

            System.out.println(users);
        } catch (Exception exception) {
            exception.printStackTrace();
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }

        }

    }

    @Test
    public void PreparedStatement_executeQuery() {
        Connection connection = null;

        try {
            connection = 连接.getConnection();
            String sql = "SELECT id, name, password FROM user;";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            ResultSet resultSet = preparedStatement.executeQuery();

            ArrayList<User> users = new ArrayList<>();
            while (resultSet.next()) {
                User user = new User(resultSet.getInt("id"), resultSet.getString("name"), resultSet.getString("password"));
                users.add(user);
            }

            System.out.println(users);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
    }

    @Test
    public void PreparedStatement_executeUpdate(){
        Connection connection = null;

        try {
            connection = 连接.getConnection();

            String sql = "update user set password = '123456' where name = ?";

            PreparedStatement preparedStatement = connection.prepareStatement(sql);

            preparedStatement.setString(1, "王康");

            int i = preparedStatement.executeUpdate();
            System.out.println(i);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (connection != null){
                try {
                    connection.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
    }
}
